package com.naxian.service.Impl;

import com.github.pagehelper.PageInfo;
import com.naxian.mapper.ResumesMapper;
import com.naxian.pojo.Persons;
import com.naxian.pojo.Resumes;
import com.naxian.pojo.ResumesExample;
import com.naxian.service.resumeWrite.ResumesService;
import com.naxian.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResumesServiceImpl implements ResumesService {
    @Autowired
    private ResumesMapper resumesMapper;


    @Override
    public boolean save(Resumes resumes) {
        //连接数据库
        int rows = resumesMapper.insert(resumes);
        return rows > 0;
    }

    @Override
    public boolean update(Resumes resumes) {

        //连接数据库
        int rows = resumesMapper.updateByPrimaryKey(resumes);
        return rows > 0;
    }

    @Override
    public List<Resumes> getByPerId(int perId) {
        ResumesExample example = new ResumesExample();
        ResumesExample.Criteria criteria = example.createCriteria();
        criteria.andPerIdEqualTo(perId);
        criteria.andResumesStatusEqualTo(1);
        List<Resumes> resumesList = resumesMapper.selectByExample(example);
        System.out.println("Service层中根据perId查询到的简历集合" + resumesList);
        if (resumesList.size() > 0) {
            //如果查询的数据库不为空那么，证明数据库简历表中中存在此用户id
            Resumes resumes = resumesList.get(0);
            return resumesList;
        } else {
            return resumesList;//1.表示收据库中用户不存在，用户可以注册
        }


    }

    @Override
    public boolean insert(Resumes resumes) {

        //连接数据库
        int rows = resumesMapper.insert(resumes);
        return rows > 0;
    }

    @Override
    public JsonResult getByTelno(Long perTel) {
        return new JsonResult(0, "", null);
    }

    /**
     * 根据简历Id查询简历
     *
     * @param resumeId
     * @return
     */
    @Override
    public List<Resumes> getByResumeId(Integer resumeId) {
        ResumesExample resumesExample = new ResumesExample();
        ResumesExample.Criteria criteria = resumesExample.createCriteria();
        criteria.andResumeIdEqualTo(resumeId);
        criteria.andResumesStatusEqualTo(1);
        List<Resumes> resumesList = resumesMapper.selectByExample(resumesExample);
        System.out.println("Sevice层中投递简历时查找出来的简历信息" + resumesList);
        return resumesList;
    }

    @Override
    public JsonResult getByPerName(String perName) {
        System.out.println(perName);
        Persons persons = null;


        return null;
    }

    /**
     * 删除简历（更改简历的状态）
     *
     * @param resumeId
     * @return
     */
    @Override
    public JsonResult delectByResumeId(Integer resumeId) {
        ResumesExample resumesExample = new ResumesExample();
        ResumesExample.Criteria criteria = resumesExample.createCriteria();
        criteria.andResumeIdEqualTo(resumeId);
        int rows = 0;
        List<Resumes> resumesList = resumesMapper.selectByExample(resumesExample);
        if (resumesList != null) {
            for (Resumes resumes : resumesList) {
                resumes.setResumesStatus(0);
                rows = this.resumesMapper.updateByPrimaryKey(resumes);
            }
            return new JsonResult(1, "删除成功", rows);
        }
        return new JsonResult(0, "删除失败", rows);
    }

    /**
     * 根据简历创建时间查询简历Id
     *
     * @param resumeCreateTime
     * @return
     */
    @Override
    public Integer getByResumeCreateTime(Date resumeCreateTime) {
        ResumesExample resumesExample = new ResumesExample();
        ResumesExample.Criteria criteria = resumesExample.createCriteria();
        criteria.andResumeCreatetimeEqualTo(resumeCreateTime);
        List<Resumes> resumesList = resumesMapper.selectByExample(resumesExample);
        Integer resumeId = null;
        if (resumesList == null) {
            System.out.println("查询失败没有此时间创建的简历");
        } else {
            Resumes resumes = resumesList.get(0);
            resumeId = resumes.getResumeId();
        }
        return resumeId;
    }
}